package com.niuwa.p2p.action.customerservice;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.entity.custservice.CustServiceUser;
import com.niuwa.p2p.entity.custservice.ImportCustRule;
import com.niuwa.p2p.service.custService.ICustServiceService;
import com.niuwa.p2p.service.custService.IImportCustRuleService;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author futianliang
 * @date 2015年4月29日
 */
public class CustomerServiceAction extends JsonAction {
	private static final long serialVersionUID = -8900124777631535966L;
	/** 日志 */
	private static Logger logger = Logger.getLogger(CustomerServiceAction.class);
	private ImportCustRule cusRule;
	@Autowired
	private ICustServiceService custServiceService;



	public ICustServiceService getCustServiceService() {
		return custServiceService;
	}

	public void setCustServiceService(ICustServiceService custServiceService) {
		this.custServiceService = custServiceService;
	}

	public ImportCustRule getCusRule() {
		return cusRule;
	}

	public void setCusRule(ImportCustRule cusRule) {
		this.cusRule = cusRule;
	}

	@Autowired
	private IImportCustRuleService rultService;

	@Action("goToImportRegisterRult")
	public String goToImportRegisterRult() {
		ActionContext context = ActionContext.getContext();
		List<ImportCustRule> rules = rultService.getImportCustRuleList();
		ImportCustRule rule = new ImportCustRule();
		if (!CollectionUtils.isEmpty(rules)) {
			rule = rules.get(0);
		}
		context.put("cusRule", rule);
		return "importRegisterRule";

	}
	
	
	

	@Action("ImportRegisterRult")
	public String importRegisterRult() {
		JsonResult results = new JsonResult();
		String message = "";
		try {
			if (cusRule != null && cusRule.getId() != null) {
				rultService.updateByPrimaryKey(cusRule);
				message = "success";
			} else if (cusRule != null) {
				cusRule.setId(1l);
				rultService.insert(cusRule);
				message = "success";
			}
			results.setMsg(message);
		} catch (Exception e) {
			message = "修改失败";
			results.setMsg(message);
			e.printStackTrace();
		} finally {
			setResult(results);
		}
		return NONE;

	}
	
	/*
	 * 
	 *跳转到增加客户
	 * @return String
	 *
	 */
	@Action(value="gotoAddCustomerPage", 
			results={@Result(location="addCustomer.jsp", name=SUCCESS)})
	public String gotoAddCustomerPage(){
		return SUCCESS;
	}
	
	
	
	/**
	 * 
	 *增加客户
	 *return String
	 */
	@Action(value="addCustomer")
	public String addCustomer(){
		String message=new String();
        JsonResult results=new JsonResult();	
     // 获取查询参数
     		Map<String, Object> paramMap = getParamMap();
	try {
		List<CustServiceUser> custServiceUserList=custServiceService.getCustServiceUseByTel(paramMap);
		 if(custServiceUserList.size()!=0){
			 message="手机重复,新增失败";
			 throw new RuntimeException();
		 }else{
			 custServiceService.addCustServiceUser(paramMap);	
			 message="新增成功";
			 results.setMsg(message);
			 setResult(results);
		 }
	} catch (Exception e) {
		if(!("手机重复,新增失败".equalsIgnoreCase(message))){
		   message="新增失败";
		}
		 results.setMsg(message);
		  setResult(results);
		  e.printStackTrace();	
	}
	return NONE;            

}
	

}
